﻿
namespace SUIE
{
    using System;
    using System.Collections.Generic;
    using System.Web.UI.WebControls.WebParts;
    using Microsoft.SharePoint.WebPartPages;
    using SUIE.ControlTemplates;
    using SUIE;

    public class TabConfigurationEditorPart : EditorPart
    {
        // The deployment path of the User Control
        const string TabControlConfigurationPath = @"~/_CONTROLTEMPLATES/SUIE/TabConfigurationEditorPartUserControl.ascx";

        // The User Control Object ID
        const string UserControlID = "OperationsUserControl";

        //public Boolean TabsEnabled { get; set; }

        // Declare a reference to the User Control
        TabConfigurationEditorPartUserControl configuratorControl;

        public TabConfigurationEditorPart()
        {
            this.Title = "Configuration";            
        }

        public override bool ApplyChanges()
        {
            base.EnsureChildControls();

            var wb = this.WebPartToEdit as Tabber;
            if (wb == null) return false;

            // Set the Web Part's Properties
            wb.TabsEnabled = this.configuratorControl.TabsEnabled;
            wb.TabOrientation = this.configuratorControl.TabOrientation;
            wb.TabTheme = this.configuratorControl.TabTheme;

            // Call the webpart's personalization dirty            
            //this.tabEditorWebPart.SaveChanges();

            return true;
        }

        public override void SyncChanges()
        {
            // sync with the new property changes here
            base.EnsureChildControls();
                        
            var wb = this.WebPartToEdit as Tabber;
            if (wb == null) return;
            
        }

        protected override void CreateChildControls()
        {
            base.CreateChildControls();

            // Load the User Control and it to the Controls Collection of the Editor Part
            this.configuratorControl =
                this.Page.LoadControl(TabConfigurationEditorPart.TabControlConfigurationPath) as TabConfigurationEditorPartUserControl;
            this.configuratorControl.ID = TabConfigurationEditorPart.UserControlID;

            var wb = this.WebPartToEdit as Tabber;
            if (wb == null) return;
            
            // set the user control properties, from the web part
            this.configuratorControl.TabsEnabled = wb.TabsEnabled;
            this.configuratorControl.TabOrientation = wb.TabOrientation;
            this.configuratorControl.TabTheme = wb.TabTheme;
            this.Controls.Add(configuratorControl);
        }

    }
}
